var IsLocked = false;
function DoUnlock() {
	IsLocked = false;
}

$(window).unbind('keydown').bind('keydown', function(event) {
	event.preventDefault();
	if(!YumeHikkiEngine.Player)
		return;
	if(IsLocked)
		return;
	IsLocked = true;
	t2=setTimeout("DoUnlock()", YHConstants.events.locktime);
	var isScene = (YumeHikkiEngine.Scene.Type == YHConstants.scene.mode.scene);
	switch (event.keyCode) {
		case YHConstants.keys.left: // Move Left
			var newPos = YumeHikkiEngine.Player.getLeft() - YumeHikkiEngine.Player.getSpeed()
			YumeHikkiEngine.Player.moveX(newPos, 'left');
			if(isScene) {
				YumeHikkiEngine.ScrollX(newPos, 'left');
			}
			break;
		case YHConstants.keys.right: // Move Right
			var newPos = YumeHikkiEngine.Player.getLeft() + YumeHikkiEngine.Player.getSpeed();
			YumeHikkiEngine.Player.moveX(newPos, 'right');
			if(isScene) {
				YumeHikkiEngine.ScrollX(newPos, 'right');
			}
		break;
		case YHConstants.keys.up: // Move Up
			var newPos = YumeHikkiEngine.Player.getTop() - YumeHikkiEngine.Player.getSpeed();
			YumeHikkiEngine.Player.moveY(newPos, 'up');
			if(isScene) {
				YumeHikkiEngine.ScrollY(newPos, 'up');
			}
		break;
		case YHConstants.keys.down: // Move Down
			var newPos = YumeHikkiEngine.Player.getTop() + YumeHikkiEngine.Player.getSpeed();
			YumeHikkiEngine.Player.moveY(newPos, 'down');
			if(isScene) {
				YumeHikkiEngine.ScrollY(newPos, 'down');
			}
		break;
		case YHConstants.keys.enter: // Do Action
			YumeHikkiEngine.ActionAt(YumeHikkiEngine.Player.getCenter());
		break;
	}
});
$(window).unbind('keyup').bind('keyup', function(event) {
	event.preventDefault();
	if(!YumeHikkiEngine.Player)
		return;
	YumeHikkiEngine.Player.Stop();
});
$.extend({
	getUrlVars: function(){
		var vars = [], hash;
		var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
		var hashesLength = hashes.length;
		var i = 0
		while(i < hashesLength)
		{
			hash = hashes[i].split('=');
			vars.push(hash[0]);
			vars[hash[0]] = hash[1];
			i++;
		}
		return vars;
	},
	UrlVars : null,
	getUrlVar : function(key) {
		if(!key)
			return null;
		if(!this.UrlVars)
			this.UrlVars = this.getUrlVars();
		if(typeof(this.UrlVars[key]) == "undefined")
			return null;
		else
			return this.UrlVars[key];
	}
});

function showMessage(txt, delay){
	var del = (delay) ? delay : YHConstants.textarea.delay * 2;
	showInTextArea('#textarea', txt);
}
function showThink(txt, delay){
	var del = (delay) ? delay : YHConstants.textarea.delay;
	showInTextArea('#think', txt);
}
function showInTextArea(area, txt, delay){
	$(area).html(txt).fadeIn("slow").delay(delay).fadeOut("slow");
}